{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from unicodedata import normalize\n",
    "from tensorflow import keras\n",
    "import os\n",
    "from tensorflow.keras.models import Sequential, load_model\n",
    "from tensorflow.keras.optimizers import RMSprop\n",
    "from collections import Counter\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from matplotlib import pyplot as plt\n",
    "from tensorflow.keras.layers import Conv2D, ZeroPadding2D, AveragePooling2D, BatchNormalization, Activation, Dense, \\\n",
    "    Input, MaxPooling2D,Flatten,Dropout\n",
    "from tensorflow.keras import Model, layers, regularizers\n",
    "import tensorflow.keras.backend as K\n",
    "from tensorflow.keras.datasets import cifar10\n",
    "from tensorflow.keras.callbacks import LearningRateScheduler, ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\n",
    "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
    "from tensorflow.keras.applications.resnet50 import preprocess_input\n",
    "from tensorflow.keras.initializers import he_normal\n",
    "from tensorflow.keras import optimizers\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn import preprocessing\n",
    "from sklearn.metrics import accuracy_score\n",
    "from tqdm import tqdm\n",
    "from tensorflow.keras.models import load_model\n",
    "import csv\n",
    "import cv2\n",
    "#importing model \n",
    "from tensorflow.keras.applications import ResNet50, densenet,VGG16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = csv.reader(open('../../data/data_set/Car_label.csv','r',encoding='utf-8'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_class = 49"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_x = []\n",
    "test_x = []\n",
    "train_y = []\n",
    "test_y = []\n",
    "X_data = []\n",
    "Y_data = []\n",
    "input_shape=(96,96,3)#3通道图像数据\n",
    "for lists in f:\n",
    "    img = cv2.imread('../../data/data_set/'+lists[1])\n",
    "    img = cv2.resize(img, (input_shape[0], input_shape[1]))\n",
    "    X_data.append(img)\n",
    "    Y_data.append(int(lists[2]))\n",
    "#     if lists[3] == '1':\n",
    "#         train_x.append(img)\n",
    "#         train_y.append(int(lists[2]))\n",
    "#     else:\n",
    "#         test_x.append(img)\n",
    "#         test_y.append(int(lists[2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_x,test_x,train_y,test_y = train_test_split(X_data,Y_data,test_size=0.1)\n",
    "train_x = np.array(train_x).astype('float32') / 255.\n",
    "train_y = np.array(train_y)\n",
    "test_x = np.array(test_x).astype('float32') / 255.\n",
    "test_y = np.array(test_y)\n",
    "# train_x_mean = np.mean(train_x, axis=0)\n",
    "# test_x_mean = np.mean(test_x, axis=0)\n",
    "# train_x -= train_x_mean\n",
    "# test_x -= test_x_mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_x = np.array(train_x)\n",
    "train_y = np.array(train_y)\n",
    "test_x = np.array(test_x)\n",
    "test_y = np.array(test_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "lb = preprocessing.LabelBinarizer().fit(np.array(range(num_class)))  # 对标签进行ont_hot编码\n",
    "train_y = lb.transform(train_y)  # 因为是多分类任务，必须进行编码处理\n",
    "test_y = lb.transform(test_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def build_model_with_resnet50(freeze_wights=False):\n",
    "\n",
    "    Backbone = ResNet50(\n",
    "    include_top=False, weights='imagenet', pooling='avg')\n",
    "\n",
    "    if freeze_wights:\n",
    "        Backbone.trainable = False\n",
    "        for layer in Backbone.layers:\n",
    "            if \"BatchNormalization\" in layer.__class__.__name__:\n",
    "                layer.trainable = True\n",
    "            else:\n",
    "                layer.trainable = False\n",
    "    else:\n",
    "        Backbone.trainable = True\n",
    "\n",
    "    X = BatchNormalization()(Backbone.output)\n",
    "\n",
    "    X = Flatten(name=\"flatten\")(X)\n",
    "    \n",
    "    X = Dense(1024)(X)\n",
    "    X = Activation('relu')(X) \n",
    "\n",
    "    X = BatchNormalization()(X)\n",
    "\n",
    "    X = Dense(num_class)(X)\n",
    "    X = Activation('softmax')(X)\n",
    "\n",
    "    model = Model(Backbone.input, X)\n",
    "\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def build_model_with_denseNet121(freeze_wights=False):\n",
    "\n",
    "    Backbone = densenet.DenseNet121(\n",
    "    include_top=False, weights='imagenet', pooling='avg')\n",
    "\n",
    "    if freeze_wights:\n",
    "        Backbone.trainable = False\n",
    "        for layer in Backbone.layers:\n",
    "            if \"BatchNormalization\" in layer.__class__.__name__:\n",
    "                layer.trainable = True\n",
    "            else:\n",
    "                layer.trainable = False\n",
    "    else:\n",
    "        Backbone.trainable = True\n",
    "\n",
    "    X = BatchNormalization()(Backbone.output)\n",
    "\n",
    "    X = Flatten(name=\"flatten\")(X)\n",
    "    \n",
    "    X = Dense(1024)(X)\n",
    "    X = Activation('relu')(X) \n",
    "\n",
    "    X = BatchNormalization()(X)\n",
    "\n",
    "    X = Dense(num_class)(X)\n",
    "    X = Activation('softmax')(X)\n",
    "\n",
    "    model = Model(Backbone.input, X)\n",
    "\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "ResNet_model = build_model_with_denseNet121()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "loss = 'categorical_crossentropy'\n",
    "optimizer = optimizers.Adam(lr=1e-4)\n",
    "ResNet_model.compile(loss=loss , optimizer= optimizer, metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# sgd = optimizers.SGD(lr=0.01, decay=1e-4, momentum=0.9, nesterov=True)\n",
    "optimizer = optimizers.Adam(lr=1e-4)\n",
    "ResNet_model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['acc'])\n",
    "\n",
    "# change_lr = LearningRateScheduler(scheduler)\n",
    "reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=6, verbose=1, min_delta=1e-6)\n",
    "\n",
    "save_dir = os.path.join(os.getcwd(), '../../data/trained_model')\n",
    "model_name = 'denseNet121_model.{epoch:02d}-{val_acc:.2f}.h5'\n",
    "if not os.path.isdir(save_dir):\n",
    "    os.makedirs(save_dir)\n",
    "filepath = os.path.join(save_dir, model_name)\n",
    "\n",
    "checkpoint = ModelCheckpoint(filepath=filepath,\n",
    "                             monitor='val_acc',\n",
    "                             verbose=1,\n",
    "                             save_best_only=True)\n",
    "\n",
    "#creating early stopping to prevent model from overfitting \n",
    "early_stopping = EarlyStopping(monitor=\"val_acc\", min_delta=0,\n",
    "                                                  patience=16, verbose=1, \n",
    "                                                  mode=\"auto\", baseline=None, \n",
    "                                                  restore_best_weights=True)\n",
    "\n",
    "cbks = [early_stopping, checkpoint, reduce_lr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data_gen = ImageDataGenerator(rotation_range=40,\n",
    "    zoom_range=0.2,\n",
    "    width_shift_range=0.2,\n",
    "    height_shift_range=0.2,\n",
    "    shear_range=0.2,\n",
    "    horizontal_flip=True,\n",
    "    fill_mode=\"nearest\")\n",
    "valid_data_gen = ImageDataGenerator()\n",
    "train_data_gen.fit(train_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 4.1333 - acc: 0.0822\n",
      "Epoch 00001: val_acc improved from -inf to 0.11550, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.01-0.12.h5\n",
      "251/251 [==============================] - 25s 98ms/step - loss: 4.1333 - acc: 0.0822 - val_loss: 4.0203 - val_acc: 0.1155\n",
      "Epoch 2/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 3.2714 - acc: 0.2075\n",
      "Epoch 00002: val_acc improved from 0.11550 to 0.20074, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.02-0.20.h5\n",
      "251/251 [==============================] - 21s 85ms/step - loss: 3.2714 - acc: 0.2075 - val_loss: 3.9291 - val_acc: 0.2007\n",
      "Epoch 3/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.8089 - acc: 0.2864\n",
      "Epoch 00003: val_acc improved from 0.20074 to 0.25015, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.03-0.25.h5\n",
      "251/251 [==============================] - 21s 85ms/step - loss: 2.8089 - acc: 0.2864 - val_loss: 3.2392 - val_acc: 0.2502\n",
      "Epoch 4/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.4414 - acc: 0.3560\n",
      "Epoch 00004: val_acc improved from 0.25015 to 0.31563, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.04-0.32.h5\n",
      "251/251 [==============================] - 21s 85ms/step - loss: 2.4414 - acc: 0.3560 - val_loss: 3.0228 - val_acc: 0.3156\n",
      "Epoch 5/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.1276 - acc: 0.4172\n",
      "Epoch 00005: val_acc improved from 0.31563 to 0.34651, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.05-0.35.h5\n",
      "251/251 [==============================] - 21s 85ms/step - loss: 2.1276 - acc: 0.4172 - val_loss: 3.0289 - val_acc: 0.3465\n",
      "Epoch 6/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.9240 - acc: 0.4680\n",
      "Epoch 00006: val_acc improved from 0.34651 to 0.38233, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.06-0.38.h5\n",
      "251/251 [==============================] - 21s 85ms/step - loss: 1.9240 - acc: 0.4680 - val_loss: 2.4985 - val_acc: 0.3823\n",
      "Epoch 7/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.7575 - acc: 0.5026\n",
      "Epoch 00007: val_acc improved from 0.38233 to 0.46634, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.07-0.47.h5\n",
      "251/251 [==============================] - 21s 85ms/step - loss: 1.7575 - acc: 0.5026 - val_loss: 2.0537 - val_acc: 0.4663\n",
      "Epoch 8/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.5989 - acc: 0.5383\n",
      "Epoch 00008: val_acc did not improve from 0.46634\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 1.5989 - acc: 0.5383 - val_loss: 2.2147 - val_acc: 0.4497\n",
      "Epoch 9/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.4732 - acc: 0.5779\n",
      "Epoch 00009: val_acc improved from 0.46634 to 0.48857, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.09-0.49.h5\n",
      "251/251 [==============================] - 21s 85ms/step - loss: 1.4732 - acc: 0.5779 - val_loss: 2.0079 - val_acc: 0.4886\n",
      "Epoch 10/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.3522 - acc: 0.6132\n",
      "Epoch 00010: val_acc improved from 0.48857 to 0.52193, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.10-0.52.h5\n",
      "251/251 [==============================] - 21s 85ms/step - loss: 1.3522 - acc: 0.6132 - val_loss: 1.8033 - val_acc: 0.5219\n",
      "Epoch 11/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.2553 - acc: 0.6325\n",
      "Epoch 00011: val_acc did not improve from 0.52193\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 1.2553 - acc: 0.6325 - val_loss: 1.8429 - val_acc: 0.5158\n",
      "Epoch 12/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.1479 - acc: 0.6661\n",
      "Epoch 00012: val_acc improved from 0.52193 to 0.56146, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.12-0.56.h5\n",
      "251/251 [==============================] - 21s 85ms/step - loss: 1.1479 - acc: 0.6661 - val_loss: 1.7320 - val_acc: 0.5615\n",
      "Epoch 13/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.0546 - acc: 0.6858\n",
      "Epoch 00013: val_acc improved from 0.56146 to 0.56763, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.13-0.57.h5\n",
      "251/251 [==============================] - 21s 85ms/step - loss: 1.0546 - acc: 0.6858 - val_loss: 1.6543 - val_acc: 0.5676\n",
      "Epoch 14/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.9909 - acc: 0.7067\n",
      "Epoch 00014: val_acc improved from 0.56763 to 0.59296, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.14-0.59.h5\n",
      "251/251 [==============================] - 21s 86ms/step - loss: 0.9909 - acc: 0.7067 - val_loss: 1.5128 - val_acc: 0.5930\n",
      "Epoch 15/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.9717 - acc: 0.7063\n",
      "Epoch 00015: val_acc did not improve from 0.59296\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.9717 - acc: 0.7063 - val_loss: 1.8938 - val_acc: 0.5386\n",
      "Epoch 16/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.8858 - acc: 0.7336\n",
      "Epoch 00016: val_acc improved from 0.59296 to 0.60902, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.16-0.61.h5\n",
      "251/251 [==============================] - 22s 86ms/step - loss: 0.8858 - acc: 0.7336 - val_loss: 1.5717 - val_acc: 0.6090\n",
      "Epoch 17/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.8597 - acc: 0.7389\n",
      "Epoch 00017: val_acc improved from 0.60902 to 0.62322, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.17-0.62.h5\n",
      "251/251 [==============================] - 21s 86ms/step - loss: 0.8597 - acc: 0.7389 - val_loss: 1.4407 - val_acc: 0.6232\n",
      "Epoch 18/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.7668 - acc: 0.7636\n",
      "Epoch 00018: val_acc did not improve from 0.62322\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.7668 - acc: 0.7636 - val_loss: 1.5376 - val_acc: 0.6047\n",
      "Epoch 19/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.7559 - acc: 0.7680\n",
      "Epoch 00019: val_acc did not improve from 0.62322\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.7559 - acc: 0.7680 - val_loss: 1.9898 - val_acc: 0.5318\n",
      "Epoch 20/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.6714 - acc: 0.7962\n",
      "Epoch 00020: val_acc did not improve from 0.62322\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.6714 - acc: 0.7962 - val_loss: 1.5427 - val_acc: 0.6035\n",
      "Epoch 21/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.6686 - acc: 0.7949\n",
      "Epoch 00021: val_acc improved from 0.62322 to 0.66275, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.21-0.66.h5\n",
      "251/251 [==============================] - 22s 86ms/step - loss: 0.6686 - acc: 0.7949 - val_loss: 1.3725 - val_acc: 0.6628\n",
      "Epoch 22/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.6223 - acc: 0.8085\n",
      "Epoch 00022: val_acc did not improve from 0.66275\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.6223 - acc: 0.8085 - val_loss: 1.3050 - val_acc: 0.6418\n",
      "Epoch 23/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5761 - acc: 0.8215\n",
      "Epoch 00023: val_acc did not improve from 0.66275\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.5761 - acc: 0.8215 - val_loss: 1.4465 - val_acc: 0.6294\n",
      "Epoch 24/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5564 - acc: 0.8287\n",
      "Epoch 00024: val_acc did not improve from 0.66275\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.5564 - acc: 0.8287 - val_loss: 1.5193 - val_acc: 0.6010\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 25/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5335 - acc: 0.8293\n",
      "Epoch 00025: val_acc improved from 0.66275 to 0.67264, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.25-0.67.h5\n",
      "251/251 [==============================] - 22s 86ms/step - loss: 0.5335 - acc: 0.8293 - val_loss: 1.2951 - val_acc: 0.6726\n",
      "Epoch 26/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.4999 - acc: 0.8490\n",
      "Epoch 00026: val_acc did not improve from 0.67264\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.4999 - acc: 0.8490 - val_loss: 1.3528 - val_acc: 0.6572\n",
      "Epoch 27/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.4768 - acc: 0.8500\n",
      "Epoch 00027: val_acc did not improve from 0.67264\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.4768 - acc: 0.8500 - val_loss: 1.6900 - val_acc: 0.6103\n",
      "Epoch 28/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.4623 - acc: 0.8551\n",
      "Epoch 00028: val_acc did not improve from 0.67264\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.4623 - acc: 0.8551 - val_loss: 1.4797 - val_acc: 0.6448\n",
      "Epoch 29/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.4599 - acc: 0.8521\n",
      "Epoch 00029: val_acc did not improve from 0.67264\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.4599 - acc: 0.8521 - val_loss: 1.4252 - val_acc: 0.6553\n",
      "Epoch 30/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.4438 - acc: 0.8652\n",
      "Epoch 00030: val_acc did not improve from 0.67264\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.4438 - acc: 0.8652 - val_loss: 1.5849 - val_acc: 0.6325\n",
      "Epoch 31/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.3944 - acc: 0.8757\n",
      "Epoch 00031: val_acc did not improve from 0.67264\n",
      "\n",
      "Epoch 00031: ReduceLROnPlateau reducing learning rate to 4.999999873689376e-05.\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.3944 - acc: 0.8757 - val_loss: 1.3853 - val_acc: 0.6665\n",
      "Epoch 32/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.3109 - acc: 0.9051\n",
      "Epoch 00032: val_acc improved from 0.67264 to 0.71402, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.32-0.71.h5\n",
      "251/251 [==============================] - 22s 86ms/step - loss: 0.3109 - acc: 0.9051 - val_loss: 1.1812 - val_acc: 0.7140\n",
      "Epoch 33/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2874 - acc: 0.9109\n",
      "Epoch 00033: val_acc did not improve from 0.71402\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.2874 - acc: 0.9109 - val_loss: 1.1624 - val_acc: 0.7029\n",
      "Epoch 34/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2597 - acc: 0.9247\n",
      "Epoch 00034: val_acc did not improve from 0.71402\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.2597 - acc: 0.9247 - val_loss: 1.2738 - val_acc: 0.6868\n",
      "Epoch 35/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2242 - acc: 0.9334\n",
      "Epoch 00035: val_acc did not improve from 0.71402\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.2242 - acc: 0.9334 - val_loss: 1.2216 - val_acc: 0.7122\n",
      "Epoch 36/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2406 - acc: 0.9256\n",
      "Epoch 00036: val_acc did not improve from 0.71402\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.2406 - acc: 0.9256 - val_loss: 1.1866 - val_acc: 0.7103\n",
      "Epoch 37/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2154 - acc: 0.9345\n",
      "Epoch 00037: val_acc did not improve from 0.71402\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.2154 - acc: 0.9345 - val_loss: 1.2821 - val_acc: 0.6967\n",
      "Epoch 38/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2148 - acc: 0.9363\n",
      "Epoch 00038: val_acc improved from 0.71402 to 0.71896, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.38-0.72.h5\n",
      "251/251 [==============================] - 22s 86ms/step - loss: 0.2148 - acc: 0.9363 - val_loss: 1.1719 - val_acc: 0.7190\n",
      "Epoch 39/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2039 - acc: 0.9379\n",
      "Epoch 00039: val_acc did not improve from 0.71896\n",
      "\n",
      "Epoch 00039: ReduceLROnPlateau reducing learning rate to 2.499999936844688e-05.\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.2039 - acc: 0.9379 - val_loss: 1.2339 - val_acc: 0.7091\n",
      "Epoch 40/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1751 - acc: 0.9483\n",
      "Epoch 00040: val_acc improved from 0.71896 to 0.72576, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.40-0.73.h5\n",
      "251/251 [==============================] - 22s 86ms/step - loss: 0.1751 - acc: 0.9483 - val_loss: 1.1472 - val_acc: 0.7258\n",
      "Epoch 41/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1681 - acc: 0.9537\n",
      "Epoch 00041: val_acc improved from 0.72576 to 0.73255, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.41-0.73.h5\n",
      "251/251 [==============================] - 22s 86ms/step - loss: 0.1681 - acc: 0.9537 - val_loss: 1.1429 - val_acc: 0.7326\n",
      "Epoch 42/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1659 - acc: 0.9492\n",
      "Epoch 00042: val_acc improved from 0.73255 to 0.73379, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.42-0.73.h5\n",
      "251/251 [==============================] - 21s 86ms/step - loss: 0.1659 - acc: 0.9492 - val_loss: 1.0960 - val_acc: 0.7338\n",
      "Epoch 43/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1424 - acc: 0.9583\n",
      "Epoch 00043: val_acc did not improve from 0.73379\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.1424 - acc: 0.9583 - val_loss: 1.1278 - val_acc: 0.7245\n",
      "Epoch 44/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1413 - acc: 0.9614\n",
      "Epoch 00044: val_acc improved from 0.73379 to 0.74120, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.44-0.74.h5\n",
      "251/251 [==============================] - 22s 86ms/step - loss: 0.1413 - acc: 0.9614 - val_loss: 1.0925 - val_acc: 0.7412\n",
      "Epoch 45/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1297 - acc: 0.9638\n",
      "Epoch 00045: val_acc improved from 0.74120 to 0.74182, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.45-0.74.h5\n",
      "251/251 [==============================] - 22s 86ms/step - loss: 0.1297 - acc: 0.9638 - val_loss: 1.1045 - val_acc: 0.7418\n",
      "Epoch 46/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1322 - acc: 0.9629\n",
      "Epoch 00046: val_acc did not improve from 0.74182\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.1322 - acc: 0.9629 - val_loss: 1.1145 - val_acc: 0.7356\n",
      "Epoch 47/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1309 - acc: 0.9590\n",
      "Epoch 00047: val_acc did not improve from 0.74182\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.1309 - acc: 0.9590 - val_loss: 1.1153 - val_acc: 0.7363\n",
      "Epoch 48/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1257 - acc: 0.9639\n",
      "Epoch 00048: val_acc did not improve from 0.74182\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.1257 - acc: 0.9639 - val_loss: 1.1680 - val_acc: 0.7270\n",
      "Epoch 49/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1172 - acc: 0.9664\n",
      "Epoch 00049: val_acc improved from 0.74182 to 0.74305, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.49-0.74.h5\n",
      "251/251 [==============================] - 22s 86ms/step - loss: 0.1172 - acc: 0.9664 - val_loss: 1.0651 - val_acc: 0.7431\n",
      "Epoch 50/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1167 - acc: 0.9637\n",
      "Epoch 00050: val_acc did not improve from 0.74305\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.1167 - acc: 0.9637 - val_loss: 1.1670 - val_acc: 0.7375\n",
      "Epoch 51/200\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "251/251 [==============================] - ETA: 0s - loss: 0.1123 - acc: 0.9668\n",
      "Epoch 00051: val_acc did not improve from 0.74305\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.1123 - acc: 0.9668 - val_loss: 1.1023 - val_acc: 0.7431\n",
      "Epoch 52/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1197 - acc: 0.9643\n",
      "Epoch 00052: val_acc improved from 0.74305 to 0.75664, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\denseNet121_model.52-0.76.h5\n",
      "251/251 [==============================] - 22s 86ms/step - loss: 0.1197 - acc: 0.9643 - val_loss: 1.0434 - val_acc: 0.7566\n",
      "Epoch 53/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1145 - acc: 0.9673\n",
      "Epoch 00053: val_acc did not improve from 0.75664\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.1145 - acc: 0.9673 - val_loss: 1.1567 - val_acc: 0.7363\n",
      "Epoch 54/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1100 - acc: 0.9663\n",
      "Epoch 00054: val_acc did not improve from 0.75664\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.1100 - acc: 0.9663 - val_loss: 1.1179 - val_acc: 0.7443\n",
      "Epoch 55/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1000 - acc: 0.9720\n",
      "Epoch 00055: val_acc did not improve from 0.75664\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.1000 - acc: 0.9720 - val_loss: 1.1552 - val_acc: 0.7344\n",
      "Epoch 56/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1071 - acc: 0.9689\n",
      "Epoch 00056: val_acc did not improve from 0.75664\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.1071 - acc: 0.9689 - val_loss: 1.1125 - val_acc: 0.7375\n",
      "Epoch 57/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1123 - acc: 0.9668\n",
      "Epoch 00057: val_acc did not improve from 0.75664\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.1123 - acc: 0.9668 - val_loss: 1.0967 - val_acc: 0.7511\n",
      "Epoch 58/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.1083 - acc: 0.9699\n",
      "Epoch 00058: val_acc did not improve from 0.75664\n",
      "\n",
      "Epoch 00058: ReduceLROnPlateau reducing learning rate to 1.249999968422344e-05.\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.1083 - acc: 0.9699 - val_loss: 1.1025 - val_acc: 0.7505\n",
      "Epoch 59/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.0939 - acc: 0.9754\n",
      "Epoch 00059: val_acc did not improve from 0.75664\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.0939 - acc: 0.9754 - val_loss: 1.0839 - val_acc: 0.7431\n",
      "Epoch 60/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.0940 - acc: 0.9721\n",
      "Epoch 00060: val_acc did not improve from 0.75664\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.0940 - acc: 0.9721 - val_loss: 1.0827 - val_acc: 0.7492\n",
      "Epoch 61/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.0822 - acc: 0.9784\n",
      "Epoch 00061: val_acc did not improve from 0.75664\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.0822 - acc: 0.9784 - val_loss: 1.0481 - val_acc: 0.7523\n",
      "Epoch 62/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.0857 - acc: 0.9763\n",
      "Epoch 00062: val_acc did not improve from 0.75664\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.0857 - acc: 0.9763 - val_loss: 1.0491 - val_acc: 0.7542\n",
      "Epoch 63/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.0824 - acc: 0.9760\n",
      "Epoch 00063: val_acc did not improve from 0.75664\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.0824 - acc: 0.9760 - val_loss: 1.0611 - val_acc: 0.7449\n",
      "Epoch 64/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.0824 - acc: 0.9758\n",
      "Epoch 00064: val_acc did not improve from 0.75664\n",
      "\n",
      "Epoch 00064: ReduceLROnPlateau reducing learning rate to 6.24999984211172e-06.\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.0824 - acc: 0.9758 - val_loss: 1.0925 - val_acc: 0.7449\n",
      "Epoch 65/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.0751 - acc: 0.9793\n",
      "Epoch 00065: val_acc did not improve from 0.75664\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.0751 - acc: 0.9793 - val_loss: 1.0786 - val_acc: 0.7498\n",
      "Epoch 66/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.0713 - acc: 0.9806\n",
      "Epoch 00066: val_acc did not improve from 0.75664\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.0713 - acc: 0.9806 - val_loss: 1.0842 - val_acc: 0.7474\n",
      "Epoch 67/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.0649 - acc: 0.9831\n",
      "Epoch 00067: val_acc did not improve from 0.75664\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.0649 - acc: 0.9831 - val_loss: 1.0819 - val_acc: 0.7486\n",
      "Epoch 68/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.0709 - acc: 0.9796Restoring model weights from the end of the best epoch.\n",
      "\n",
      "Epoch 00068: val_acc did not improve from 0.75664\n",
      "251/251 [==============================] - 21s 83ms/step - loss: 0.0709 - acc: 0.9796 - val_loss: 1.0651 - val_acc: 0.7486\n",
      "Epoch 00068: early stopping\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x148e6005d30>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "batch_size = 32\n",
    "epochs = 200\n",
    "iterations = 251\n",
    "ResNet_model.fit(train_data_gen.flow(train_x, train_y, batch_size=batch_size),\n",
    "                    steps_per_epoch=iterations,\n",
    "                    epochs=epochs,\n",
    "                    callbacks=cbks,\n",
    "                    validation_data=valid_data_gen.flow(test_x, test_y, batch_size=batch_size))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "def build_model_with_vgg16(freeze_wights=False):\n",
    "\n",
    "    Backbone = VGG16(\n",
    "    include_top=False, weights='imagenet', pooling='avg')\n",
    "\n",
    "    if freeze_wights:\n",
    "        Backbone.trainable = False\n",
    "        for layer in Backbone.layers:\n",
    "            if \"BatchNormalization\" in layer.__class__.__name__:\n",
    "                layer.trainable = True\n",
    "            else:\n",
    "                layer.trainable = False\n",
    "    else:\n",
    "        Backbone.trainable = True\n",
    "\n",
    "    X = BatchNormalization()(Backbone.output)\n",
    "\n",
    "    X = Flatten(name=\"flatten\")(X)\n",
    "    \n",
    "    X = Dense(1024)(X)\n",
    "    X = Activation('relu')(X) \n",
    "\n",
    "    X = BatchNormalization()(X)\n",
    "\n",
    "    X = Dense(num_class)(X)\n",
    "    X = Activation('softmax')(X)\n",
    "\n",
    "    model = Model(Backbone.input, X)\n",
    "\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"functional_9\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "input_5 (InputLayer)         [(None, None, None, 3)]   0         \n",
      "_________________________________________________________________\n",
      "block1_conv1 (Conv2D)        (None, None, None, 64)    1792      \n",
      "_________________________________________________________________\n",
      "block1_conv2 (Conv2D)        (None, None, None, 64)    36928     \n",
      "_________________________________________________________________\n",
      "block1_pool (MaxPooling2D)   (None, None, None, 64)    0         \n",
      "_________________________________________________________________\n",
      "block2_conv1 (Conv2D)        (None, None, None, 128)   73856     \n",
      "_________________________________________________________________\n",
      "block2_conv2 (Conv2D)        (None, None, None, 128)   147584    \n",
      "_________________________________________________________________\n",
      "block2_pool (MaxPooling2D)   (None, None, None, 128)   0         \n",
      "_________________________________________________________________\n",
      "block3_conv1 (Conv2D)        (None, None, None, 256)   295168    \n",
      "_________________________________________________________________\n",
      "block3_conv2 (Conv2D)        (None, None, None, 256)   590080    \n",
      "_________________________________________________________________\n",
      "block3_conv3 (Conv2D)        (None, None, None, 256)   590080    \n",
      "_________________________________________________________________\n",
      "block3_pool (MaxPooling2D)   (None, None, None, 256)   0         \n",
      "_________________________________________________________________\n",
      "block4_conv1 (Conv2D)        (None, None, None, 512)   1180160   \n",
      "_________________________________________________________________\n",
      "block4_conv2 (Conv2D)        (None, None, None, 512)   2359808   \n",
      "_________________________________________________________________\n",
      "block4_conv3 (Conv2D)        (None, None, None, 512)   2359808   \n",
      "_________________________________________________________________\n",
      "block4_pool (MaxPooling2D)   (None, None, None, 512)   0         \n",
      "_________________________________________________________________\n",
      "block5_conv1 (Conv2D)        (None, None, None, 512)   2359808   \n",
      "_________________________________________________________________\n",
      "block5_conv2 (Conv2D)        (None, None, None, 512)   2359808   \n",
      "_________________________________________________________________\n",
      "block5_conv3 (Conv2D)        (None, None, None, 512)   2359808   \n",
      "_________________________________________________________________\n",
      "block5_pool (MaxPooling2D)   (None, None, None, 512)   0         \n",
      "_________________________________________________________________\n",
      "global_average_pooling2d (Gl (None, 512)               0         \n",
      "_________________________________________________________________\n",
      "batch_normalization_8 (Batch (None, 512)               2048      \n",
      "_________________________________________________________________\n",
      "flatten (Flatten)            (None, 512)               0         \n",
      "_________________________________________________________________\n",
      "dense_8 (Dense)              (None, 1024)              525312    \n",
      "_________________________________________________________________\n",
      "activation_8 (Activation)    (None, 1024)              0         \n",
      "_________________________________________________________________\n",
      "batch_normalization_9 (Batch (None, 1024)              4096      \n",
      "_________________________________________________________________\n",
      "dense_9 (Dense)              (None, 49)                50225     \n",
      "_________________________________________________________________\n",
      "activation_9 (Activation)    (None, 49)                0         \n",
      "=================================================================\n",
      "Total params: 15,296,369\n",
      "Trainable params: 15,293,297\n",
      "Non-trainable params: 3,072\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model = build_model_with_vgg16()\n",
    "model.summary()#显示模型结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "loss = 'categorical_crossentropy'\n",
    "optimizer = optimizers.Adam(lr=1e-4)\n",
    "model.compile(loss=loss , optimizer= optimizer, metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "# sgd = optimizers.SGD(lr=0.01, decay=1e-4, momentum=0.9, nesterov=True)\n",
    "optimizer = optimizers.Adam(lr=1e-4)\n",
    "model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['acc'])\n",
    "\n",
    "# change_lr = LearningRateScheduler(scheduler)\n",
    "reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1, min_delta=1e-6)\n",
    "\n",
    "save_dir = os.path.join(os.getcwd(), '../../data/trained_model')\n",
    "model_name = 'vgg16_model.{epoch:02d}-{val_acc:.2f}.h5'\n",
    "if not os.path.isdir(save_dir):\n",
    "    os.makedirs(save_dir)\n",
    "filepath = os.path.join(save_dir, model_name)\n",
    "\n",
    "checkpoint = ModelCheckpoint(filepath=filepath,\n",
    "                             monitor='val_acc',\n",
    "                             verbose=1,\n",
    "                             save_best_only=True)\n",
    "\n",
    "#creating early stopping to prevent model from overfitting \n",
    "early_stopping = EarlyStopping(monitor=\"val_acc\", min_delta=0,\n",
    "                                                  patience=8, verbose=1, \n",
    "                                                  mode=\"auto\", baseline=None, \n",
    "                                                  restore_best_weights=True)\n",
    "\n",
    "cbks = [early_stopping, checkpoint, reduce_lr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data_gen = ImageDataGenerator(rotation_range=40,\n",
    "    zoom_range=0.2,\n",
    "    width_shift_range=0.2,\n",
    "    height_shift_range=0.2,\n",
    "    shear_range=0.2,\n",
    "    horizontal_flip=True,\n",
    "    fill_mode=\"nearest\")\n",
    "valid_data_gen = ImageDataGenerator()\n",
    "train_data_gen.fit(train_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/200\n",
      "  2/251 [..............................] - ETA: 10s - loss: 4.7475 - acc: 0.0156WARNING:tensorflow:Callbacks method `on_train_batch_end` is slow compared to the batch time (batch time: 0.0290s vs `on_train_batch_end` time: 0.0578s). Check your callbacks.\n",
      "251/251 [==============================] - ETA: 0s - loss: 4.1820 - acc: 0.0421\n",
      "Epoch 00001: val_acc improved from -inf to 0.01729, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.01-0.02.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 4.1820 - acc: 0.0421 - val_loss: 9.4788 - val_acc: 0.0173\n",
      "Epoch 2/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 3.7634 - acc: 0.0814\n",
      "Epoch 00002: val_acc improved from 0.01729 to 0.02162, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.02-0.02.h5\n",
      "251/251 [==============================] - 24s 95ms/step - loss: 3.7634 - acc: 0.0814 - val_loss: 11.9120 - val_acc: 0.0216\n",
      "Epoch 3/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 3.6116 - acc: 0.1025- ETA: 1s - loss: 3.61\n",
      "Epoch 00003: val_acc did not improve from 0.02162\n",
      "251/251 [==============================] - 23s 92ms/step - loss: 3.6116 - acc: 0.1025 - val_loss: 8.2669 - val_acc: 0.0148\n",
      "Epoch 4/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 3.4421 - acc: 0.1301- ETA\n",
      "Epoch 00004: val_acc improved from 0.02162 to 0.07906, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.04-0.08.h5\n",
      "251/251 [==============================] - 23s 94ms/step - loss: 3.4421 - acc: 0.1301 - val_loss: 4.5920 - val_acc: 0.0791\n",
      "Epoch 5/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 3.3101 - acc: 0.1551\n",
      "Epoch 00005: val_acc did not improve from 0.07906\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 3.3101 - acc: 0.1551 - val_loss: 4.4978 - val_acc: 0.0630\n",
      "Epoch 6/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 3.1398 - acc: 0.1780\n",
      "Epoch 00006: val_acc improved from 0.07906 to 0.09018, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.06-0.09.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 3.1398 - acc: 0.1780 - val_loss: 3.7468 - val_acc: 0.0902\n",
      "Epoch 7/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.9479 - acc: 0.2151- ETA: 1s - loss: 2.9\n",
      "Epoch 00007: val_acc improved from 0.09018 to 0.11674, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.07-0.12.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 2.9479 - acc: 0.2151 - val_loss: 4.3405 - val_acc: 0.1167\n",
      "Epoch 8/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.8184 - acc: 0.2437\n",
      "Epoch 00008: val_acc improved from 0.11674 to 0.20445, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.08-0.20.h5\n",
      "251/251 [==============================] - 23s 94ms/step - loss: 2.8184 - acc: 0.2437 - val_loss: 3.7574 - val_acc: 0.2044\n",
      "Epoch 9/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.6150 - acc: 0.2895\n",
      "Epoch 00009: val_acc improved from 0.20445 to 0.22051, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.09-0.22.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 2.6150 - acc: 0.2895 - val_loss: 3.0891 - val_acc: 0.2205\n",
      "Epoch 10/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.4507 - acc: 0.3256\n",
      "Epoch 00010: val_acc improved from 0.22051 to 0.29339, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.10-0.29.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 2.4507 - acc: 0.3256 - val_loss: 2.8567 - val_acc: 0.2934\n",
      "Epoch 11/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.2905 - acc: 0.3632- ETA: 2s - lo\n",
      "Epoch 00011: val_acc improved from 0.29339 to 0.33045, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.11-0.33.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 2.2905 - acc: 0.3632 - val_loss: 2.4638 - val_acc: 0.3305\n",
      "Epoch 12/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.1133 - acc: 0.4048\n",
      "Epoch 00012: val_acc improved from 0.33045 to 0.36566, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.12-0.37.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 2.1133 - acc: 0.4048 - val_loss: 2.4225 - val_acc: 0.3657\n",
      "Epoch 13/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.9823 - acc: 0.4312- ETA: 0s - loss: 1.9874 - a\n",
      "Epoch 00013: val_acc improved from 0.36566 to 0.37492, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.13-0.37.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 1.9823 - acc: 0.4312 - val_loss: 2.5397 - val_acc: 0.3749\n",
      "Epoch 14/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.8513 - acc: 0.4760\n",
      "Epoch 00014: val_acc improved from 0.37492 to 0.43113, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.14-0.43.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 1.8513 - acc: 0.4760 - val_loss: 2.2548 - val_acc: 0.4311\n",
      "Epoch 15/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.7537 - acc: 0.4993\n",
      "Epoch 00015: val_acc improved from 0.43113 to 0.49846, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.15-0.50.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 1.7537 - acc: 0.4993 - val_loss: 1.7036 - val_acc: 0.4985\n",
      "Epoch 16/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.6186 - acc: 0.5227\n",
      "Epoch 00016: val_acc did not improve from 0.49846\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 1.6186 - acc: 0.5227 - val_loss: 2.3133 - val_acc: 0.4262\n",
      "Epoch 17/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.5398 - acc: 0.5572\n",
      "Epoch 00017: val_acc improved from 0.49846 to 0.50093, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.17-0.50.h5\n",
      "251/251 [==============================] - 23s 94ms/step - loss: 1.5398 - acc: 0.5572 - val_loss: 1.9048 - val_acc: 0.5009\n",
      "Epoch 18/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.4228 - acc: 0.5933\n",
      "Epoch 00018: val_acc improved from 0.50093 to 0.55343, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.18-0.55.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 1.4228 - acc: 0.5933 - val_loss: 1.6493 - val_acc: 0.5534\n",
      "Epoch 19/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.3556 - acc: 0.6029\n",
      "Epoch 00019: val_acc improved from 0.55343 to 0.61211, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.19-0.61.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 1.3556 - acc: 0.6029 - val_loss: 1.3928 - val_acc: 0.6121\n",
      "Epoch 20/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.2548 - acc: 0.6262\n",
      "Epoch 00020: val_acc did not improve from 0.61211\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 1.2548 - acc: 0.6262 - val_loss: 1.5626 - val_acc: 0.5806\n",
      "Epoch 21/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.2177 - acc: 0.6435- ETA: 6s - loss: 1.2266 - - ETA: 5s - loss: 1.\n",
      "Epoch 00021: val_acc did not improve from 0.61211\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 1.2177 - acc: 0.6435 - val_loss: 1.7771 - val_acc: 0.5522\n",
      "Epoch 22/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.1369 - acc: 0.6596\n",
      "Epoch 00022: val_acc improved from 0.61211 to 0.63990, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.22-0.64.h5\n",
      "\n",
      "Epoch 00022: ReduceLROnPlateau reducing learning rate to 9.999999747378752e-06.\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 1.1369 - acc: 0.6596 - val_loss: 1.4157 - val_acc: 0.6399\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 23/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.8732 - acc: 0.7395\n",
      "Epoch 00023: val_acc improved from 0.63990 to 0.72761, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.23-0.73.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.8732 - acc: 0.7395 - val_loss: 0.9353 - val_acc: 0.7276\n",
      "Epoch 24/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.7859 - acc: 0.7668- ETA: 2s \n",
      "Epoch 00024: val_acc did not improve from 0.72761\n",
      "251/251 [==============================] - 23s 94ms/step - loss: 0.7859 - acc: 0.7668 - val_loss: 0.9331 - val_acc: 0.7196\n",
      "Epoch 25/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.7561 - acc: 0.7790\n",
      "Epoch 00025: val_acc improved from 0.72761 to 0.73996, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.25-0.74.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.7561 - acc: 0.7790 - val_loss: 0.8730 - val_acc: 0.7400\n",
      "Epoch 26/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.7157 - acc: 0.7871\n",
      "Epoch 00026: val_acc improved from 0.73996 to 0.74058, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.26-0.74.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.7157 - acc: 0.7871 - val_loss: 0.8948 - val_acc: 0.7406\n",
      "Epoch 27/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.7012 - acc: 0.7923\n",
      "Epoch 00027: val_acc improved from 0.74058 to 0.74861, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.27-0.75.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.7012 - acc: 0.7923 - val_loss: 0.8592 - val_acc: 0.7486\n",
      "Epoch 28/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.6931 - acc: 0.7897\n",
      "Epoch 00028: val_acc did not improve from 0.74861\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.6931 - acc: 0.7897 - val_loss: 0.8591 - val_acc: 0.7468\n",
      "Epoch 29/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.6579 - acc: 0.8028\n",
      "Epoch 00029: val_acc improved from 0.74861 to 0.75602, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.29-0.76.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.6579 - acc: 0.8028 - val_loss: 0.8242 - val_acc: 0.7560\n",
      "Epoch 30/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.6413 - acc: 0.8078\n",
      "Epoch 00030: val_acc improved from 0.75602 to 0.75911, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.30-0.76.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.6413 - acc: 0.8078 - val_loss: 0.8347 - val_acc: 0.7591\n",
      "Epoch 31/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.6227 - acc: 0.8128\n",
      "Epoch 00031: val_acc did not improve from 0.75911\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.6227 - acc: 0.8128 - val_loss: 0.8229 - val_acc: 0.7579\n",
      "Epoch 32/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.6073 - acc: 0.8160\n",
      "Epoch 00032: val_acc improved from 0.75911 to 0.75973, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.32-0.76.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.6073 - acc: 0.8160 - val_loss: 0.8357 - val_acc: 0.7597\n",
      "Epoch 33/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.6058 - acc: 0.8164- ETA: 2s - los\n",
      "Epoch 00033: val_acc did not improve from 0.75973\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.6058 - acc: 0.8164 - val_loss: 0.8296 - val_acc: 0.7554\n",
      "Epoch 34/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5781 - acc: 0.8223\n",
      "Epoch 00034: val_acc did not improve from 0.75973\n",
      "\n",
      "Epoch 00034: ReduceLROnPlateau reducing learning rate to 9.999999747378752e-07.\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5781 - acc: 0.8223 - val_loss: 0.8297 - val_acc: 0.7573\n",
      "Epoch 35/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5703 - acc: 0.8278\n",
      "Epoch 00035: val_acc improved from 0.75973 to 0.76961, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.35-0.77.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.5703 - acc: 0.8278 - val_loss: 0.8120 - val_acc: 0.7696\n",
      "Epoch 36/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5545 - acc: 0.8360\n",
      "Epoch 00036: val_acc did not improve from 0.76961\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5545 - acc: 0.8360 - val_loss: 0.8042 - val_acc: 0.7690\n",
      "Epoch 37/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5522 - acc: 0.8320\n",
      "Epoch 00037: val_acc improved from 0.76961 to 0.77764, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.37-0.78.h5\n",
      "251/251 [==============================] - 23s 94ms/step - loss: 0.5522 - acc: 0.8320 - val_loss: 0.7947 - val_acc: 0.7776\n",
      "Epoch 38/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5454 - acc: 0.8386- ETA: 1s - loss: 0.\n",
      "Epoch 00038: val_acc did not improve from 0.77764\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5454 - acc: 0.8386 - val_loss: 0.7929 - val_acc: 0.7727\n",
      "Epoch 39/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5557 - acc: 0.8289\n",
      "Epoch 00039: val_acc did not improve from 0.77764\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5557 - acc: 0.8289 - val_loss: 0.8010 - val_acc: 0.7684\n",
      "Epoch 40/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5434 - acc: 0.8360\n",
      "Epoch 00040: val_acc did not improve from 0.77764\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5434 - acc: 0.8360 - val_loss: 0.8076 - val_acc: 0.7665\n",
      "Epoch 41/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5457 - acc: 0.8360\n",
      "Epoch 00041: val_acc did not improve from 0.77764\n",
      "\n",
      "Epoch 00041: ReduceLROnPlateau reducing learning rate to 9.999999974752428e-08.\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5457 - acc: 0.8360 - val_loss: 0.8018 - val_acc: 0.7678\n",
      "Epoch 42/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5404 - acc: 0.8382\n",
      "Epoch 00042: val_acc did not improve from 0.77764\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5404 - acc: 0.8382 - val_loss: 0.7983 - val_acc: 0.7678\n",
      "Epoch 43/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5328 - acc: 0.8384\n",
      "Epoch 00043: val_acc did not improve from 0.77764\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5328 - acc: 0.8384 - val_loss: 0.7978 - val_acc: 0.7671\n",
      "Epoch 44/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5434 - acc: 0.8380\n",
      "Epoch 00044: val_acc did not improve from 0.77764\n",
      "\n",
      "Epoch 00044: ReduceLROnPlateau reducing learning rate to 1.0000000116860975e-08.\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5434 - acc: 0.8380 - val_loss: 0.7958 - val_acc: 0.7678\n",
      "Epoch 45/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5370 - acc: 0.8379Restoring model weights from the end of the best epoch.\n",
      "\n",
      "Epoch 00045: val_acc did not improve from 0.77764\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.5370 - acc: 0.8379 - val_loss: 0.7971 - val_acc: 0.7690\n",
      "Epoch 00045: early stopping\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x2947e3e20f0>"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "batch_size = 32\n",
    "epochs = 200\n",
    "iterations = 251\n",
    "model.fit(train_data_gen.flow(train_x, train_y, batch_size=batch_size),\n",
    "                    steps_per_epoch=iterations,\n",
    "                    epochs=epochs,\n",
    "                    callbacks=cbks,\n",
    "                    validation_data=valid_data_gen.flow(test_x, test_y, batch_size=batch_size))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sgd = optimizers.SGD(lr=0.1, momentum=0.9, nesterov=True)\n",
    "model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['acc'])\n",
    "\n",
    "change_lr = LearningRateScheduler(scheduler)\n",
    "\n",
    "save_dir = os.path.join(os.getcwd(), 'data/trained_model')\n",
    "model_name = 'vgg19_model.{epoch:02d}-{val_acc:.2f}.h5'\n",
    "if not os.path.isdir(save_dir):\n",
    "    os.makedirs(save_dir)\n",
    "filepath = os.path.join(save_dir, model_name)\n",
    "\n",
    "checkpoint = ModelCheckpoint(filepath=filepath,\n",
    "                             monitor='val_acc',\n",
    "                             verbose=1,\n",
    "                             save_best_only=True)\n",
    "\n",
    "#creating early stopping to prevent model from overfitting \n",
    "early_stopping = EarlyStopping(monitor=\"val_acc\", min_delta=0,\n",
    "                                                  patience=10, verbose=1, \n",
    "                                                  mode=\"auto\", baseline=None, \n",
    "                                                  restore_best_weights=True)\n",
    "\n",
    "cbks = [early_stopping, checkpoint, change_lr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def scheduler(epoch):\n",
    "    if epoch < 80:\n",
    "        return 0.01\n",
    "    if epoch < 160:\n",
    "        return 0.005\n",
    "    return 0.001"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Using real-time data augmentation.')\n",
    "# datagen = ImageDataGenerator(horizontal_flip=True,\n",
    "#                              width_shift_range=0.125, height_shift_range=0.125, fill_mode='constant', cval=0.)\n",
    "datagen = ImageDataGenerator()\n",
    "valid_data_gen = ImageDataGenerator()\n",
    "datagen.fit(train_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_x = []\n",
    "test_x = []\n",
    "train_y = []\n",
    "test_y = []\n",
    "X_data = []\n",
    "Y_data = []\n",
    "input_shape=(224,224,3)#3通道图像数据\n",
    "for lists in f:\n",
    "    img = cv2.imread('../../data/data_set/'+lists[1])\n",
    "    img = cv2.resize(img, (input_shape[0], input_shape[1]))\n",
    "    X_data.append(img)\n",
    "    Y_data.append(int(lists[2]))\n",
    "#     if lists[3] == '1':\n",
    "#         train_x.append(img)\n",
    "#         train_y.append(int(lists[2]))\n",
    "#     else:\n",
    "#         test_x.append(img)\n",
    "#         test_y.append(int(lists[2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_x = np.array(train_x).astype('float32') / 255.\n",
    "train_y = np.array(train_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = load_model('../../data/neural_networks/Car_DenseNet121.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_x_perdict = model(test_x[:512])\n",
    "test_x_perdict_arg = np.argmax(test_x_perdict, axis=1)\n",
    "test_y_arg = np.argmax(test_y[:512], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import classification_report\n",
    "report = classification_report(test_y_arg,test_x_perdict_arg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00         1\n",
      "           1       0.89      0.73      0.80        22\n",
      "           2       0.83      1.00      0.91         5\n",
      "           3       0.92      1.00      0.96        36\n",
      "           4       0.92      0.88      0.90        25\n",
      "           5       1.00      1.00      1.00        15\n",
      "           6       1.00      1.00      1.00         3\n",
      "           7       0.94      1.00      0.97        17\n",
      "           8       0.80      0.80      0.80         5\n",
      "           9       0.85      0.89      0.87        65\n",
      "          10       0.78      0.82      0.80        22\n",
      "          12       0.92      0.92      0.92        38\n",
      "          13       1.00      1.00      1.00         5\n",
      "          14       1.00      0.75      0.86         4\n",
      "          15       1.00      1.00      1.00        14\n",
      "          16       1.00      1.00      1.00         5\n",
      "          17       0.91      0.91      0.91        33\n",
      "          18       0.88      0.79      0.83        19\n",
      "          19       1.00      0.75      0.86         4\n",
      "          20       1.00      1.00      1.00         1\n",
      "          21       1.00      1.00      1.00         7\n",
      "          22       0.84      1.00      0.92        27\n",
      "          23       1.00      0.50      0.67         2\n",
      "          24       1.00      1.00      1.00         4\n",
      "          25       0.50      0.50      0.50         2\n",
      "          26       0.93      1.00      0.97        14\n",
      "          27       1.00      0.89      0.94         9\n",
      "          28       1.00      0.75      0.86         4\n",
      "          30       1.00      1.00      1.00         3\n",
      "          31       1.00      1.00      1.00         3\n",
      "          32       1.00      0.67      0.80         3\n",
      "          33       1.00      1.00      1.00         1\n",
      "          34       0.93      0.93      0.93        27\n",
      "          35       1.00      1.00      1.00         1\n",
      "          36       1.00      1.00      1.00         8\n",
      "          37       0.80      1.00      0.89         4\n",
      "          38       0.50      1.00      0.67         1\n",
      "          39       1.00      1.00      1.00         4\n",
      "          40       1.00      1.00      1.00         2\n",
      "          41       1.00      0.67      0.80         6\n",
      "          42       1.00      1.00      1.00         5\n",
      "          43       0.67      0.67      0.67         6\n",
      "          44       1.00      1.00      1.00         2\n",
      "          45       1.00      0.83      0.91        12\n",
      "          46       1.00      0.89      0.94         9\n",
      "          47       0.88      1.00      0.93         7\n",
      "\n",
      "    accuracy                           0.91       512\n",
      "   macro avg       0.93      0.90      0.91       512\n",
      "weighted avg       0.91      0.91      0.91       512\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(report)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "json_config = model.to_json()\n",
    "json_config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('DenseNet_model.json', 'w') as json_file:\n",
    "    json_file.write(json_config)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "grad",
   "language": "python",
   "name": "grad"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
